﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


/// <summary>
/// Descripción breve de BD
/// </summary>
public class BD
{
    private SqlConnection sqlCon;
	public BD()
	{
        sqlCon = new SqlConnection();

	}

    public bool AbrirConexion()
    {
      try
        {
          //La linea de ConfigurationManager.ConnectionStrings saca la cadena de conexion del archivo
          //web.config, 
            sqlCon = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["BDSUEConnectionString1"].ToString());
            sqlCon.Open();
            return true;
        }
        catch 
        { return false; }
       
    }

    
    public void CerrarConexion()
    {
        sqlCon.Close();
    }

    public string Insertar(string tabla, string campos, string valores)
    {
        string sql = ("Insert into " + tabla +" "+ campos +" "+"values" + valores);
        try
        {
            SqlCommand sqlcmd = new SqlCommand(sql, sqlCon);
            sqlcmd.ExecuteNonQuery();
            return sql;
        }
        catch(SqlException  ex)
        { return ex.Message; }
    }

    public DataTable Consultar(string consulta)
    {
        DataSet ds = new System.Data.DataSet();
        SqlDataAdapter sqla = new SqlDataAdapter(consulta, sqlCon);
        try
        {
            sqla.Fill(ds); 
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
        return ds.Tables[0];
    }

    public string Eliminar(string tabla, string condicion)
    {
           
        string sql = ("delete from " + tabla + " " + " where " + condicion);
        try
        {
            SqlCommand sqlcmd = new SqlCommand(sql, sqlCon);
            sqlcmd.ExecuteNonQuery();
            return sql;
                 
        }
        catch
        { return "No se pudo realizar la Eliminación"; }
    }

    public string Actualizar(string tabla, string campValor, string condicion)
    {

        string sql = ("update " + tabla + " set " + campValor +" where " + condicion);
        try
        {
            SqlCommand sqlcmd = new SqlCommand(sql, sqlCon);
            sqlcmd.ExecuteNonQuery();
            return sql;

        }
        catch
        { return "No se pudo realizar la Actualización"; }
    }


}
